/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package factory;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import javax.swing.Timer;
import org.newdawn.slick.Color;

/**
 *
 * @author wida46984148
 */
public class PGame extends BasicGameState {
    int puntuacion = 0;
    Timer timer;
    public static final int ID = 0;
    Image imatgeFons;
    StateBasedGame state;
    aux2 a = new aux2();
    float x = 350; // en original 800x600
    //float x = 150;
    float y = 800;
    float zxc = -1300;
    int nivel;
    float enemigolvl;
    //boolean gamestart = false;
    boolean gameend = false;
    boolean gamepause = true;
    NauBase nbW = null;
    NauBase[] AnbW = new NauBase[3];
    NauBase nbE = null;
    NauBase nbBoss = null;
    Shoot msE = null;
    Shoot ms2 = null;
    int id = 0;
    int cnt;
    int disparosr = 0;      //disparos realizados contador pol sabra
    boolean enemigo = true;
    boolean pause = false;
    final String[] oleadaEnemigos = {"nave0","nave0D","nave0D","boss1"};
    //final String[] oleadaEnemigos = {"nave0"};
    int enemigoF = 0;
    //nave0(nave) nave1(tank) nave2(heli)

    public void init(GameContainer container, StateBasedGame game) throws SlickException {
        this.state = game;
        imatgeFons = new Image("src/data/superLand.png");
        nivel = 1;
        switch (nivel) {
            case 1:
                nbW = Naves.nausWida("nave2");
                a.posicionaUnitatAmiga(x, y, nbW);
                enemigolvl = 0.4f;
                break;
            case 2:
                enemigolvl = 0.5f;
                break;
            case 3:
                nbW = Naves.nausWida("nave3");
                a.posicionaUnitatAmiga(x, y, nbW);
                enemigolvl = 0.6f;
                break;
        }

    }

    public void update(GameContainer gc, StateBasedGame game, int delta) throws SlickException {
        Input input = gc.getInput();
        
        if (nbW.posY > 648) {
            nbW.posY -= 5;
            nbW.setLocation(nbW.posX, nbW.posY);

        }
        
        if (!(nbW.destroy)) {
            if (gameend) {
                if (nbW.posY > -150) {
                    nbW.posY -= 5;
                    nbW.setLocation(nbW.posX, nbW.posY);
                } else {
                    game.enterState(9);
                }
            } else {
                if (nbW.posY > 648) {
                    nbW.posY -= 5;
                    nbW.setLocation(nbW.posX, nbW.posY);
                }
                if (input.isKeyDown(Input.KEY_A)) {
                    float auxX = nbW.posX;
                    nbW.posX += -enemigolvl * delta;
                    if (nbW.posX <= -30) {
                        nbW.posX = auxX;
                    }
                    nbW.setLocation(nbW.posX, nbW.posY);
                }

                if (input.isKeyDown(Input.KEY_D)) {
                    float auxX = nbW.posX;
                    nbW.posX += enemigolvl * delta;
                    //if (700 <= nbW.posX) { //en original 800X600
                    if (924 <= nbW.posX) { //en original 800X600
                        //                if (300 <= x) {
                        nbW.posX = auxX;
                    }
                    nbW.setLocation(nbW.posX, nbW.posY);
                }
                if (input.isKeyDown(Input.KEY_S)) {
                    float hip = enemigolvl * delta;
                    float auxY = nbW.posY;
                    x += hip * java.lang.Math.sin(java.lang.Math.toRadians(0.0));
                    nbW.posY += hip * java.lang.Math.cos(java.lang.Math.toRadians(0.0));
                    if (648 <= nbW.posY) {
                        nbW.posY = auxY;
                    }
                    nbW.setLocation(nbW.posX, nbW.posY);
                }

                if (input.isKeyDown(Input.KEY_W)) {
                    float hip = enemigolvl * delta;
                    float auxY = nbW.posY;
                    nbW.posX += hip * java.lang.Math.sin(java.lang.Math.toRadians(0.0));
                    nbW.posY -= hip * java.lang.Math.cos(java.lang.Math.toRadians(0.0));
                    if (nbW.posY <= 0) {
                        nbW.posY = auxY;
                    }
                    nbW.setLocation(nbW.posX, nbW.posY);
                }
                if ((int) java.lang.Math.sin(java.lang.Math.toRadians(cnt)) == 1 || (int) java.lang.Math.sin(java.lang.Math.toRadians(cnt)) == -1) {
                    oleadaEnemigos(1);
                }
                cnt += 2;
                if (input.isKeyPressed(Input.KEY_SPACE)) {
                    disparosr++;
                    Shoot ms = null;
                    ms = nbW.getShoot();
                    

                    a.posShoot(nbW.posX + 58, nbW.posY, ms);
                }

            }
        } else {
            nbW.setLocation(1000, 1000);
            game.enterState(5);
        }

        a.update(delta);
        //zxc += 0.2;
        zxc = zxc != 0 ? zxc += 0.3 : 0;
    }

    public void oleadaEnemigos(int x) {
        if (enemigoF < oleadaEnemigos.length) {
            a.posicionaUnitatEnemiga(oleadaEnemigos[enemigoF]);
            enemigoF += x;
        }
//        else if (enemigoF == oleadaEnemigos.length) {
//            a.posicionaUnitatEnemiga("boss1");//boss
//            enemigoF += x;
//        } else {
//            gameend = a.anbE.get(a.anbE.size() - 1).isMort();
//        }
    }

    public void render(GameContainer gc, StateBasedGame game, Graphics g)
            throws SlickException {
        imatgeFons.draw(200, zxc);
        a.dibuixar(g, gc);
        NauWida1 nb = (NauWida1) nbW;
        estatNau(gc, game, g);
        g.drawString("Puntuacion: " + nb.getPuntuancion(), 300, 10);
//        p.dibuixar(g, gc);
//        mi.render(gc, g);
    }

    public void estatNau(GameContainer gc, StateBasedGame game, Graphics g)
            throws SlickException {
        NauWida1 nb = (NauWida1) nbW;
        g.setColor(Color.blue);
        g.fillRect(0, 0, 1024, 40);
        nb.renderVida(gc, g);
        nb.renderVides(gc, g);
    }

//    @Override
//    public void enter(GameContainer gc, StateBasedGame state) {
//        p = new Tauler(6, 10, 1024, 600);
//        md = new ManagerDinersAures();
//        mi = new MenuIngame(gc, 0, 600, ManagerRecursos.getImage("contenidorIngameImage"), ManagerPerfil.getUnitatsTriades(), md, state);
//        mi.clear();
//        mi.reiniciarBotons();
//        ManagerContext.setDiners(md);
//        ManagerEnemics.iniciarCompteEnrere();
//    }
    @Override
    public int getID() {
        return ID;

    }
}
